package com.authine.cloudpivot.ext.applicationservice.basic;

import cn.hutool.core.lang.Assert;
import com.alibaba.cola.dto.SingleResponse;
import com.alibaba.cola.exception.Exceptions;
import com.alibaba.fastjson.JSONObject;
import com.authine.cloudpivot.ext.constants.ModelConst;
import com.authine.cloudpivot.ext.dto.ReminderRuleDto;
import com.authine.cloudpivot.ext.utils.BoServiceUtils;
import com.authine.mvp.app.launcher.dto.bo.query.expression.Operator;
import com.authine.mvp.app.launcher.dto.bo.response.BO;
import com.authine.mvp.app.launcher.dto.bo.response.BOList;
import com.authine.mvp.app.launcher.dto.errorcode.ErrorCodeEnum;
import com.google.common.collect.Maps;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.extern.slf4j.Slf4j;

import javax.ws.rs.POST;
import javax.ws.rs.Path;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import static com.authine.hermes.app.launcher.service.ApplicationServiceFoundation.CUSTOM_SERVICE;

/**
 * @ClassName ReminderRuleService
 * @Description: 提醒规则服务
 * @Author fj
 * @Date 2021/9/17
 * @Version V1.0
 **/
@Path("ReminderRuleService")
@Slf4j
public class ReminderRuleService {
    /**
     * 保存提醒规则
     *
     * @param reminderRuleList 导出信息
     * @return 下载信息
     */
    @Path("saveRemindRule")
    @POST
    @Operation(summary = "保存提醒规则", tags = CUSTOM_SERVICE)
    public SingleResponse<BO> saveRemindRule(@Parameter List<ReminderRuleDto> reminderRuleList) {
        Assert.notEmpty(reminderRuleList, "提醒规则不能为空");
        SingleResponse<BOList> remindRuleBOList;
        Map<String,Object> dataMap;
        SingleResponse<BO> remindRuleBO = null;
        for (ReminderRuleDto reminderRuleDto : reminderRuleList) {
            // 通过提醒类型和提醒事项去查询，如果不存在，新增数据。反之更新数据
            String[] columns = new String[]{"type", "remind_type_enum"};
            Operator[] operators = new Operator[]{Operator.EQ, Operator.EQ};
            Object[] columnsVal = new Object[]{reminderRuleDto.getType(), reminderRuleDto.getRemindTypeEnum()};
            remindRuleBOList = BoServiceUtils.findListByComplex(ModelConst.T_REMINDER_RULE, columns, columnsVal, operators);
            log.info("------------------------根据提醒类型和提醒类型事项查询列表数据：{}------------------------", JSONObject.toJSONString(remindRuleBOList));
            if (!remindRuleBOList.isSuccess()) {
                Exceptions.throwBizException(ErrorCodeEnum.BO_LIST_ERROR);
            }
            dataMap = Maps.newHashMap();
            if (remindRuleBOList.getData().getTotal() == 0) {
                // 新增
                dataMap.put("type", reminderRuleDto.getType());
                dataMap.put("hours", reminderRuleDto.getHours());
                dataMap.put("remind_type_enum", reminderRuleDto.getRemindTypeEnum());
                dataMap.put("description", reminderRuleDto.getDescription());
                dataMap.put("operator", null);
                remindRuleBO = BoServiceUtils.createBo(ModelConst.T_REMINDER_RULE, Collections.singletonList(dataMap));
                log.info("------------------------新增提醒规则成功：{}------------------------", JSONObject.toJSONString(remindRuleBOList));
            } else {
                // 更新
                dataMap.put("id", remindRuleBOList.getData().getData().get(0).get("id"));
                dataMap.put("hours", reminderRuleDto.getHours());
                remindRuleBO = BoServiceUtils.updateMainBo(ModelConst.T_REMINDER_RULE, dataMap);
                log.info("------------------------修改提醒规则成功：{}------------------------", JSONObject.toJSONString(remindRuleBOList));
            }
        }
        return remindRuleBO;
    }
}
